const http = require("http");
const fs = require("fs");
const path = require("path");

http.createServer((req, res) => {
    var jurl = req.url;
    console.log(jurl);

    if (jurl.indexOf("/detail") >= 0) {
        var filePath = path.join(__dirname, "/chapter.html");
        res.writeHead(200, { "Content-Type": "text/html;charset=utf-8" });
        var fileContent = fs.readFileSync(filePath);
        res.write(fileContent);
        res.end();
    } else if (jurl.indexOf("css/") >= 0) {
        var filePath = path.join(__dirname, "/" + jurl);
        var fileContent = fs.readFileSync(filePath);
        res.writeHead(200, { "Content-Type": "text/css;charset=utf-8" });
        res.write(fileContent);
        res.end();
    } else if (jurl.indexOf("/baiduTemplate.js") >= 0) {
        var filePath = path.join(__dirname, "/js/baiduTemplate.js");
        var fileContent = fs.readFileSync(filePath);
        res.writeHead(200, { "Content-Type": "text/js;charset=utf-8" });
        res.write(fileContent);
        res.end();
    } else if (jurl.indexOf("images/") >= 0) {
        var filePath = path.join(__dirname, "/" + jurl);
        var fileContent = fs.readFileSync(filePath);
        res.writeHead(200, { "Content-Type": "image/png;charset=utf-8" });
        res.write(fileContent);
        res.end();
    }
}).listen(8083);

console.log("server listening on 8083");
